package cn.wh.mapping;


import cn.wh.session.Configuration;
import cn.wh.type.JdbcType;
import cn.wh.type.TypeHandler;
import cn.wh.type.TypeHandlerRegistry;

/**
 * @Author wh
 * @Description 参数映射 #{property,javaType=int,jdbcType=NUMERIC}
 * @Date 2023/1/30 13:54
 * @Version 1.0
 **/
public class ParameterMapping {

    private Configuration configuration;

    private String property;

    private Class<?> javaType=Object.class;

    private JdbcType jdbcType;

    private TypeHandler<?> typeHandler;

    private ParameterMapping() {
    }


    public static class Builder {

        private ParameterMapping parameterMapping = new ParameterMapping();

        public Builder(Configuration configuration, String property,Class<?> javaType) {
            parameterMapping.configuration = configuration;
            parameterMapping.property = property;
            parameterMapping.javaType=javaType;
        }

        public Builder javaType(Class<?> javaType) {
            parameterMapping.javaType = javaType;
            return this;
        }

        public Builder jdbcType(JdbcType jdbcType) {
            parameterMapping.jdbcType = jdbcType;
            return this;
        }

        public ParameterMapping build(){
            if (parameterMapping.typeHandler==null && parameterMapping.javaType!=null){
                Configuration configuration = parameterMapping.configuration;
                TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
                parameterMapping.typeHandler=typeHandlerRegistry.getTypeHandler(parameterMapping.javaType,parameterMapping.jdbcType);
            }
            return parameterMapping;
        }

    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public String getProperty() {
        return property;
    }

    public Class<?> getJavaType() {
        return javaType;
    }

    public JdbcType getJdbcType() {
        return jdbcType;
    }

    public TypeHandler<?> getTypeHandler() {
        return typeHandler;
    }



}
